
$(".lds-css").css("display", "block")
// 左侧车辆到站流水线
var lis,
    cur_post_time,//当前天展示当前时间  其他天显示最早的00:00 
    train_flow_id='',//当前列车流程id
    cur_change_mes,//选中数据详情
    set_it,// 定时刷新右侧
    is_set = true
var accEmergency='',timer='',emlid=''
//左侧流水
function get_left_list(search_obj) {
    $.ajax({
        // url: serverUrl.base+serverUrl.workTaskTrain+"getTrainFlow",
        url: serverUrl.base+serverUrl.emergency+'info',
        type: "post",
        dataType:'json',
        data: search_obj,
        beforeSend: function () {     //请求成功前触发的局部事件
            $(".work_box_bottom .lds-css").css("display", "block")
        },
        success: function (res) {
            var _data= res.data
            $('.emergency_wrap .card_list li').remove()
            if(accEmergency == ''){
                accEmergency = _data[0].emergency_id
                emlid = Number(_data[0].emlid)                
            }
            get_right_mes()
            for(var i=0;i<_data.length;i++){
                var _str=''
                _str += '<li class="elm'
                if(accEmergency == _data[i].emergency_id){
                    _str += ' active';
                    //emlid = _data[i].emlid
                }
                _str += '" data-emergency-id="'+_data[i].emergency_id+'" data-emlid="'+_data[i].emlid+'" data-isactive="'+_data[i].is_active+'">'
                _str += '<div class="title">'
                _str += '<p>'+ _data[i].emergency_name+'</p>'
                _str += '</div><div class="cnt">'
                // if(_data[i].emergency_lever_id == 1){
                //     _data[i].emergency_lever_name = '一级事件'
                // }else{
                //     _data[i].emergency_lever_name = '二级事件'
                // }
                _str += '<span class="level">'+_data[i].level_name+'</span>'
                if(_data[i].is_active == '2'){
                    _str += '<span class="status"><i></i>未激活</span>'
                }else{
                    _str += '<span class="status startm"><i></i>进行中</span>'
                }      
                _str += '</div></li>'
                $('.emergency_wrap .card_list').append(_str)
            }
        },
        complete:function(){
            $(".work_box_bottom .lds-css").css("display", "none")
        },
        error:function(){
            
        }
    })
}
get_left_list({
    _: Math.random() * 100
})
// 右侧数据
function get_right_mes(uid) {
    // var emergencyid = uid.id;
    $.ajax({
        url:serverUrl.base+serverUrl.emergency+'details',
        type: "post",
        dataType:'json',
        data: {id:accEmergency,emlid:emlid},
        beforeSend: function () {     //请求成功前触发的局部事件
            $(".em_right .lds-css").css("display", "block")
        },
        success: function (res) {
            var _data = res.data,_detail=_data.details
            $(".em_cnt").find('.em_cnt_elm').remove()
            emlid = Number(_data.emlid)   
            $.each(_detail, function (i, val) {
                var _even = "",_show= ""
                if(i%2 != 0){
                    _even= "even"
                }
                if(_data.isactive == '1'){
                    _show = "acting"
                }
                var _cnt = `<div class="em_cnt_elm ${_even}">
                                <h3 class="title">步骤${i+1}<span class="working_icon" style="display: none;"> </span></h3>
                                <p class="cnt">${val.desc}</p>
                                <div class="table_wrap ${_show}">
                                    <table class="elm_table">
                                        <thead>
                                            <tr>
                                                <th width="20%">接收人</th>
                                                <th width="20%">部门</th>
                                                <th width="20%">职务</th>
                                                <th width="30%">任务完成情况</th>
                                            </tr>
                                        </thead>
                                        <tbody>`
                                        if(val.user && val.user.length != 0){
                                            for(var j=0;j<val.user.length;j++){
                                                var _status='已完成',_color='#40abf7'
                                                if(val.user[j].status == '1'){
                                                    _is_working = true
                                                    _status='进行中';
                                                    _color='#58ec9e'
                                                }else if(val.user[j].status == '0'){
                                                    _is_working = true
                                                    _status='未接收'
                                                    _color='#ec5869'
                                                }
                                                _cnt += `<tr>
                                                    <td>${val.user[j].name}</td>
                                                    <td>${val.user[j].department_name}</td>
                                                    <td>${val.user[j].user_job}</td>
                                                    <td class="status" style="color:${_color}">${_status}</td>
                                                    </tr>`
                                            }
                                        }
                                        _cnt +=`</tbody>
                                    </table>
                                </div>
                            </div>`
                $(".em_cnt").append(_cnt)
                $(".em_cnt").find('.em_cnt_elm:last').find('.working_icon').css('display','inline-block')
            })
            if(powers.see_emergency){
                if(_data.isactive == '1'){
                    $('.emergencybtn').removeClass('beginwork').text('关闭应急预案')
                    clearInterval(timer)
                    timer = setInterval(function(){
                        set_interval()
                    },5000)
                }else{
                    $('.emergencybtn').addClass('beginwork').text('激活应急预案')
                    clearInterval(timer)
                    $('.working_icon').css('display','none')
                }
            }else{
                $('.emergencybtn').prop("disabled", true).css("background", "#ccc")
            }
        },
        complete:function(){
            $(".em_right .lds-css").css("display", "none")
        }

    })
}
// 定时刷新
function set_interval() {
    $.ajax({
        url: serverUrl.base+serverUrl.emergency+'details',
        type:"post",
        dataType:"json",
        data:{id:accEmergency,emlid:emlid},
        success: function (res) {
            if(res.code == '0'){
                var _data = res.data,_detail=_data.details
                if(_data.isactive == '1' && _data.id == accEmergency){
                    var _wraps=$('.em_cnt').find('.em_cnt_elm')
                    $.each(_detail, function (i, val) {
                        var _is_working=false
                        var _detailwrap=$(_wraps[i]).find('.table_wrap .elm_table tbody');
                        if(val.user && val.user.length != 0){
                            var _trs = _detailwrap.find('tr')
                            for(var j=0;j<val.user.length;j++){
                                var _status='已完成',_color='#40abf7'
                                if(val.user[j].status == '1'){
                                    _is_working = true
                                    _status='进行中';
                                    _color='#58ec9e'
                                }else if(val.user[j].status == '0'){
                                    _is_working = true
                                    _status='未接收'
                                    _color='#ec5869'
                                }
                                if(_trs[j] && _trs[j].length != 0){
                                    //console.log($(_trs[j]),val.user[j].name)
                                    $(_trs[j]).find('td.status').css('color',_color).text(_status)
                                }else{
                                    //console.log(val.user[j].name)
                                    var _cnt = `<tr>
                                        <td>${val.user[j].name}</td>
                                        <td>${val.user[j].department_name}</td>
                                        <td>${val.user[j].user_job}</td>
                                        <td class="status" style="color:${_color}">${_status}</td>
                                        </tr>`
                                    _detailwrap.append(_cnt)
                                } 
                            }
                        }
                        if(_is_working){
                            $(_wraps[i]).find('.title .working_icon').css('display','inline-block')
                        }else{
                            $(_wraps[i]).find('.title .working_icon').css('display','none')
                        }
                    })
                }   
            }else{
                alert(res.info)
            }
            
        },
        complete:function(){

        }

    })
}

// 点击应急事件查看右侧详情
$(document).off('click.mergency_elm').on('click.mergency_elm','.emergency_wrap li.elm',function (e) {
    accEmergency = $(this).data('emergency-id')
    emlid = Number($(this).data('emlid'))
    get_right_mes()
    $(this).addClass("active").siblings().removeClass("active")
})

// 开始应急任务
$(".emergencybtn").on("click", function () {
    var _type = 'stop',$this=$(this),_isactive=2
    if($this.hasClass("beginwork")){
        _type = 'begin'
        _isactive=1
        
    }
    if(_type == 'stop'){
        $this.addClass('beginwork')
        $this.text('激活应急预案')
        
    }else if(_type == 'begin'){
        emlid += 1
        $this.removeClass('beginwork')
        $this.text('关闭应急预案')
    }
    $this.attr("disabled", true).css("background", "#ccc")
    $.ajax({
        url: serverUrl.base+serverUrl.emergency+"active",
        type: "post",
        dataType:'json',
        data: {
            userid:sessionStorage.getItem('user_id'),
            id:accEmergency,
            isactive:_isactive
        },
        success: function (res) {
            if (res.code == '0') {
                if(_type == 'begin'){
                    clearInterval(timer)
                    alert("激活应急预案成功")
                    get_left_list({
                        _: Math.random() * 100
                    })
                    timer = setInterval(function(){
                        set_interval()
                    },5000)
                }else{
                    clearInterval(timer)
                    get_left_list({
                        _: Math.random() * 100
                    })
                    alert("关闭应急预案成功")
                }
                
            } else {
                if(_type == 'begin'){
                    alert("激活应急预案成功")
                }else{
                    alert("关闭应急预案成功")
                }
            }
        },
        complete:function(){
            $this.attr("disabled", false).css("background", "#11a0f8")
        }
    })
})
var page= 1
// 按钮
$(".workbtn.history").on("click", function () {
    page =1
    $(".emergency_history").css("display", "block");
    $(".emergency_cnt").css("display", "none");
    $(".emergency_history .lds-css").css("display", "block")
    $.post(serverUrl.base+serverUrl.emergency+'emergencylist',{page:page,_:Math.random()},function(res){
        if(res.code == '0'){
            if(res.data.length !=0){
                $('.mes_list').show()
                $('.mes_list').find('.tab_mes').remove()
                var _list=``
                for(var i=0;i<res.data.length;i++){
                    _list += `<li class="tab_mes">
                    <span>${(i+1)+(page-1)*10}</span>                       
                    <span>${res.data[i].emergency_name}</span>                      
                    <span>${res.data[i].level_name}</span>                      
                    <span>${res.data[i].start_time}</span>
                    <span>${res.data[i].end_time}</span>
                </li>`
                }
                $('.mes_list ul').append(_list)
                $('.nolist').hide()
            }else{
                $('.nolist').show()
                $('.mes_list').hide()
            }
            
            var pagenation = $(res.data.result_page)
            $(".page_box").append(pagenation)
        }else{
            alert(res.info)
        }
    },'json').complete(function(){
        $(".emergency_history .lds-css").css("display", "none")
    })
})
function ajax_data(num) {
        page = num
        $(".workbtn.history").trigger('click')
}
// 跳转
function pageIndex(go_btn) {
    var chan_ipt = $(go_btn).prev(),
        page_num = Number(chan_ipt.val())
    if (!isNaN(page_num)) {
        ajax_data(page_num)
    }
}

$(".back_mergency").on("click", function () {
    $(".emergency_cnt").css("display", "block");
    $(".emergency_history").css("display", "none");
})
$(".cancel").on("click", function () {
    $(".emergency_cnt").css("display", "block");
    $(".emergency_history").css("display", "none");
})


